<script setup defer async lang="ts">
import { reactive, onMounted, ref } from 'vue'
import { bindWsList } from '@/api/user'
import { useI18n } from 'vue-i18n'
import { formatDate } from '@/utils/utils'
import { useSwipeHandlers } from '@/utils/useSwipeHandlers'

const { t } = useI18n()

// 
interface WhatsAppItem {
  whatsapp: string
  ws_status: number
  updated_time: string
  online_time: number
}

const baseData = reactive({
  condition: [0, 0],
  loading: false,
  page: 1,
  total: 0,
  dataList: [] as WhatsAppItem[],
  aircraftUrl: uni.getStorageSync('aircraftUrl'),
})

const leftClick = () => {
  const pages = getCurrentPages()
  if (pages.length <= 1) {
    history.back()
  } else {
    uni.navigateBack()
  }
}

const getHour = (seconds: number) => {
  const hours = Math.floor(seconds / 3600)
  const minutes = Math.floor((seconds % 3600) / 60)

  // 
  const formattedMinutes = minutes < 10 ? `0${minutes}` : minutes

  if (hours > 0) {
    return `${hours} ${t('Hour')} ${formattedMinutes} ${t('min')}`
  } else {
    return `${formattedMinutes} ${t('min')}`
  }
}

const getList = async () => {
  baseData.loading = true
  uni.showLoading({
    title: t('Loading'),
    icon: 'none',
  })
  
  try {
    const res = await bindWsList()
    if (res.code === 0) {
      baseData.total = res.data.total
      if (res.data.length > 0) {
        baseData.dataList = baseData.dataList.concat(res.data)
        baseData.page++
      }
    } else {
      uni.showToast({
        title: res.msg,
        icon: 'none',
      })
    }
  } catch (error) {
    console.error( error)
    uni.showToast({
      title: 'error',
      icon: 'none',
    })
  } finally {
    baseData.loading = false
    uni.hideLoading()
  }
}

// 
const handleSwipeRight = () => {
  const pages = getCurrentPages()
  if (pages.length <= 1) {
    history.back()
  } else {
    uni.navigateBack()
  }
}

useSwipeHandlers(handleSwipeRight)

onMounted(() => {
  // 
  uni.setNavigationBarTitle({
    title: 'Ttask!' + t('Earn money so easy') + '!', // 
  })
  getList()
})
</script>

<template>
  <view class="whatsapp-container">
    <!--  -->
    <view class="bg-decoration">
      <view class="circle circle-1"></view>
      <view class="circle circle-2"></view>
      <view class="circle circle-3"></view>
      <view class="floating-shape shape-1">📱</view>
      <view class="floating-shape shape-2">💬</view>
      <view class="floating-shape shape-3">🟢</view>
    </view>

    <!--  -->
    <up-navbar :title="t('Bound WhatsApp number')" @leftClick="leftClick" :autoBack="false">
      <template #left>
        <view class="nav-back-btn">
          <image class="back-icon" src="../../../static/img/left.png"></image>
        </view>
      </template>
    </up-navbar>

    <!-- WhatsApp  -->
    <view class="whatsapp-list-section">
      <view class="list-container">
        <!-- WhatsApp  -->
        <view 
          v-for="(item, index) in baseData.dataList" 
          :key="index"
          class="whatsapp-item"
        >
          <!-- WhatsApp  -->
          <view class="item-icon">
            <view class="icon-circle">
              <text class="icon-text">📱</text>
            </view>
          </view>

          <!--  -->
          <view class="item-content">
            <!-- WhatsApp  -->
            <view class="whatsapp-number">
              WhatsApp: {{ item.phone }}
            </view>

            <!--  -->
            <view class="status-info">
              <view class="status-indicator" :class="item.ws_status === 1 ? 'online' : 'offline'"></view>
              <text class="status-text">{{ item.ws_status === 1 ? t('Online') : t('Offline') }}</text>
            </view>

            <!--  -->
            <view class="details-section">
              <view class="detail-row">
                <view class="detail-label">{{ t('Updata Time') }}:</view>
                <view class="detail-value">{{ item.updated_time.split('.')[0].replace('T', ' ') }}</view>
              </view>
              <view class="detail-row">
                <view class="detail-label">{{ t('Online Duration') }}:</view>
                <view class="detail-value duration">{{ getHour(item.online_time) }}</view>
              </view>
            </view>
          </view>
        </view>

        <!--  -->
        <view v-if="!baseData.loading && baseData.dataList.length > 0" class="bottom-status">
          <view class="end-text">{{ t('Reached the End') }}</view>
        </view>

        <view v-if="baseData.loading" class="bottom-status">
          <view class="loading-text">{{ t('Loading') }}...</view>
        </view>

        <!--  -->
        <view v-if="!baseData.loading && baseData.dataList.length === 0" class="empty-state">
          <view class="empty-icon">📱</view>
          <view class="empty-text">{{ t('No Data') }}</view>
        </view>
      </view>
    </view>

    <!-- Telegram  -->
   <a :href="baseData.aircraftUrl" target="_blank">
         <img class="telegram-fixed-icon" src="@/static/teltgram.png" />
       </a>
  </view>
</template>

<style lang="scss" scoped>
.whatsapp-container {
  width: 100%;
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  position: relative;
  overflow: hidden;
}

// 
.bg-decoration {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  z-index: 1;

  .circle {
    position: absolute;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.1);
    animation: float 6s ease-in-out infinite;

    &.circle-1 {
      width: 200rpx;
      height: 200rpx;
      top: 10%;
      left: 10%;
      animation-delay: 0s;
    }

    &.circle-2 {
      width: 150rpx;
      height: 150rpx;
      top: 30%;
      right: 20%;
      animation-delay: 2s;
    }

    &.circle-3 {
      width: 100rpx;
      height: 100rpx;
      top: 75%;
      left: 20%;
      animation-delay: 4s;
    }
  }

  .floating-shape {
    position: absolute;
    font-size: 60rpx;
    opacity: 0.6;
    animation: bounce 8s ease-in-out infinite;

    &.shape-1 {
      top: 25%;
      right: 15%;
      animation-delay: 1s;
    }

    &.shape-2 {
      top: 60%;
      left: 15%;
      animation-delay: 3s;
    }

    &.shape-3 {
      top: 80%;
      right: 25%;
      animation-delay: 5s;
    }
  }
}



// WhatsApp 
.whatsapp-list-section {
  margin-top: 120rpx;
  margin: 0 20rpx;
  margin-top: 120rpx;
  position: relative;
  z-index: 2;

  .list-container {
    background: rgba(255, 255, 255, 0.95);
    backdrop-filter: blur(20px);
    // border-radius: 25rpx;
    // padding: 10rpx;
    min-height: 70vh;
    box-shadow: 0 10rpx 30rpx rgba(0, 0, 0, 0.1);

    .whatsapp-item {
      display: flex;
      align-items: flex-start;
      padding: 30rpx 20rpx;
      margin-bottom: 15rpx;
      background: rgba(255, 255, 255, 0.8);
      border-radius: 20rpx;
      box-shadow: 0 5rpx 15rpx rgba(0, 0, 0, 0.05);
      transition: all 0.3s ease;

      &:active {
        transform: scale(0.98);
      }

      .item-icon {
        margin-right: 25rpx;
        margin-top: 10rpx;

        .icon-circle {
          width: 80rpx;
          height: 80rpx;
          border-radius: 50%;
          background: linear-gradient(135deg, #25d366, #128c7e);
          display: flex;
          align-items: center;
          justify-content: center;
          box-shadow: 0 8rpx 20rpx rgba(37, 211, 102, 0.3);

          .icon-text {
            font-size: 32rpx;
          }
        }
      }

      .item-content {
        flex: 1;

        .whatsapp-number {
          font-size: 28rpx;
          color: #333;
          font-weight: 600;
          margin-bottom: 15rpx;
          line-height: 1.4;
        }

        .status-info {
          display: flex;
          align-items: center;
          margin-bottom: 20rpx;
          gap: 15rpx;

          .status-indicator {
            width: 20rpx;
            height: 20rpx;
            border-radius: 50%;

            &.online {
              background: #25d366;
              box-shadow: 0 0 10rpx rgba(37, 211, 102, 0.5);
            }

            &.offline {
              background: #c8c9cc;
            }
          }

          .status-text {
            font-size: 24rpx;
            font-weight: 600;

            &.online {
              color: #25d366;
            }

            &.offline {
              color: #c8c9cc;
            }
          }
        }

        .details-section {
          .detail-row {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 10rpx;

            .detail-label {
              font-size: 24rpx;
              color: #666;
              font-weight: 600;
            }

            .detail-value {
              font-size: 24rpx;
              color: #333;

              &.duration {
                color: #25d366;
                font-weight: 600;
              }
            }
          }

          .detail-row:last-child {
            margin-bottom: 0;
          }
        }
      }
    }

    .bottom-status {
      text-align: center;
      padding: 40rpx 0;

      .end-text,
      .loading-text {
        font-size: 26rpx;
        color: #999;
      }
    }

    .empty-state {
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      padding: 100rpx 0;

      .empty-icon {
        font-size: 120rpx;
        margin-bottom: 30rpx;
        opacity: 0.3;
      }

      .empty-text {
        font-size: 28rpx;
        color: #999;
      }
    }
  }
}


// 
@keyframes float {
  0%, 100% { transform: translateY(0px); }
  50% { transform: translateY(-20px); }
}

@keyframes bounce {
  0%, 100% { transform: translateY(0px) rotate(0deg); }
  50% { transform: translateY(-10px) rotate(5deg); }
}

@keyframes pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.1); }
}
</style>